/**
 * 本质上就是一个二维偏序，对(xi, yi)，问比xi大的且比yi小的点有多少个
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>
using namespace __gnu_pbds;



using llt = long long;
using pii = pair<int, int>;

struct _t{
    int idx;
    int x;
    int y;
    _t(int a=0,int b=0,int c=0):idx(a),x(b),y(c){}
    bool operator < (const _t & b) const {
        if(x != b.x) return x < b.x;
        if(idx != b.idx) return idx < b.idx;
        return y < b.y;
    }
};

int N;
vector<_t> A, B;
llt Ans = 0;

void CDQ(int s, int e){
    if(s >= e) return;

    int mid = (s + e) >> 1;
    CDQ(s, mid); CDQ(mid + 1, e);

    int u = s, v = mid + 1, t = s;
    while(u <= mid and v <= e){
        if(A[u].y <= A[v].y){     
            Ans += v - mid - 1;       
            B[t++] = A[u++];
        }else{      
            B[t++] = A[v++]; 
        }  
    }
    while(u <= mid) {
        Ans += v - mid - 1;
        B[t++] = A[u++];
    }
    while(v <= e){                
        B[t++] = A[v++];
    }
    copy(B.begin() + s, B.begin() + e + 1, A.begin() + s);
    return;
}

llt proc(){
    sort(A.begin() + 1, A.end());
    Ans = 0;
    CDQ(1, N);
    return Ans;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);
    int nofkase = 1;
    const array<string, 3> t {"ABC", "CAB", "BCA"};
    cin >> nofkase;
    while(nofkase--){
        cin >> N;
        A.assign(N + 1, {});
        B.assign(N + 1, {});
        for(int i=1;i<=N;++i) cin >> A[A[i].idx = i].x >> A[i].y;
        cout << proc() << "\n";
    }
    return 0;
}